<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板方法</title>
</head>
<body>
<script>
    var CaffeineBeverage=function(){

    };
    CaffeineBeverage.prototype.prepareRecipe=function(){
        this.boilWater();
        this.brew();
        this.pourOnCup();
        if(this.customerWantsCondiments()){
            //如果可以想加小料,就加上
            this.addCondiments();
        }
    };
    CaffeineBeverage.prototype.boilWater=function(){
        console.log("将水烧开!");
    };
    CaffeineBeverage.prototype.pourOnCup=function(){
        console.log("将饮料到在被子里")
    };
    CaffeineBeverage.prototype.brew=function(){
        throw new Error()
    };
    //默认加上小料
    CaffeineBeverage.prototype.customerWantsCondiments=function(){
        return true;
    };
//    下面两个函数分别是冲咖啡和冲茶所对应的函数：
    //冲咖啡
    var Coffee=function(){
        CaffeineBeverage.apply(this);
    };
    Coffee.prototype=new CaffeineBeverage();
    Coffee.prototype.brew=function(){
        console.log("从咖啡机里将咖啡倒进去!");
    };
    Coffee.prototype.addCondiments=function(){
       console.log("添加糖和牛奶");
    };
    Coffee.prototype.customerWantsCondiments=function(){
        return confirm("你想添加糖和牛奶吗？");
    };
    //冲茶叶
    var Tea=function(){
        CaffeineBeverage.apply(this);
    };
    Tea.prototype=new CaffeineBeverage();
    Tea.prototype.brew=function(){
        console.log("泡茶叶！")
    };
    Tea.prototype.addCondiments=function(){
        console.log("添加柠檬！");
    };
    Tea.prototype.customerWantsCondiments=function(){
        return confirm("你想添加柠檬吗？")
    };
    var aa=new Tea();
    aa.customerWantsCondiments();
</script>
</body>
</html>